package com.h1.staffmanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h1.commonutils.R;
import com.h1.staffmanage.entity.UserInfo;
import com.h1.staffmanage.entity.vo.StaffQuery;
import com.h1.staffmanage.service.UserInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-21
 */
@RestController
@CrossOrigin
@RequestMapping("/staffmanage/user-info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    //查询职员列表所有数据
    @GetMapping("findAll")
    public R findAllStaff() {
        List<UserInfo> list = userInfoService.list(null);
        return R.ok().data("items", list);

    }

    //逻辑删除职员
    @DeleteMapping("{id}")
    public R removeStaff(@PathVariable long id) {
        boolean flag = userInfoService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("pageStaff/{current}/{limit}")
    public R pageListStaff(@PathVariable long current,
                           @PathVariable long limit) {
        //创建page对象
        Page<UserInfo> pageTeacher = new Page<>(current, limit);


        userInfoService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();//总记录数
        List<UserInfo> records = pageTeacher.getRecords(); //数据list集合


        return R.ok().data("total", total).data("rows", records);
    }

    //4 条件查询职员带分页的方法
    @PostMapping("pageStaffCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) StaffQuery staffQuery) {
        //创建page对象
        Page<UserInfo> pageTeacher = new Page<>(current, limit);

        //构建条件
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();

        String name = staffQuery.getName();
        String nickName = staffQuery.getNickName();

        String begin = staffQuery.getBegin();
        String end = staffQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(nickName)) {
            wrapper.eq("nickName", nickName);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        userInfoService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<UserInfo> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    //添加职员方法
    @PostMapping("addStaff")
    public R addTeacher(@RequestBody UserInfo userInfo) {
        boolean save = userInfoService.save(userInfo);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据职员id进行查询
    @GetMapping("getStaff/{id}")
    public R getTeacher(@PathVariable long id) {
        UserInfo userInfo = userInfoService.getById(id);
        return R.ok().data("staff", userInfo);
    }

    //职员修改功能
    @PostMapping("updateStaff")
    public R updateTeacher(@RequestBody UserInfo userInfo) {
        boolean flag = userInfoService.updateById(userInfo);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}

